package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private StrategyRepository repository;


    @Override
    public void increaseViewnum(String sid, int num) {
        //拼接vokey
        String vokey = RedisKeys.STRATEGY_VO.join(sid);
        //判断redis里 vokey是否存在
        StrategyStatisVO vo = null;
        if (!redisTemplate.hasKey(vokey)) {
            //如果不存在，初始化
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy, vo);
            vo.setStrategyId(strategy.getId());
        } else {
            //如果存在直接获取
            String s = redisTemplate.opsForValue().get(vokey);
            //转化为对象
            vo = JSON.parseObject(s, StrategyStatisVO.class);
        }
        //阅读时+1
        vo.setViewnum(vo.getViewnum() + num);
        //存到redis里(转化成json类型)
        redisTemplate.opsForValue().set(vokey, JSON.toJSONString(vo));
    }

    public StrategyStatisVO getStrategyStatisVO(String sid) {
        String vokey = RedisKeys.STRATEGY_VO.join(sid);
        //判断redis里 vokey是否存在
        StrategyStatisVO vo = null;
        if (!redisTemplate.hasKey(vokey)) {
            //如果不存在，初始化
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy, vo);
            vo.setStrategyId(strategy.getId());
            //存到redis
            redisTemplate.opsForValue().set(vokey, JSON.toJSONString(vo));
        } else {
            //如果存在直接获取
            String s = redisTemplate.opsForValue().get(vokey);
            //转化为对象
            vo = JSON.parseObject(s, StrategyStatisVO.class);
        }

        return vo;
    }

    //抽取set方法
    public void setStrategyStatisVO(StrategyStatisVO vo) {
        String vokey = RedisKeys.STRATEGY_VO.join(vo.getStrategyId());
        redisTemplate.opsForValue().set(vokey, JSON.toJSONString(vo));
    }

    @Override
    public void increaseReplynum(String sid, int num) {
        //获取vo对象
        StrategyStatisVO vo = this.getStrategyStatisVO(sid);
        //评论数+1
        vo.setReplynum(vo.getReplynum() + num);
        //创建vo的key，再set到redis
        this.setStrategyStatisVO(vo);
    }

    @Override
    public Boolean favor(String sid, String uid) {
        //用用户id拼接uskey
        String userKey = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        //判断userKey在不在redis里，存在就获取，不存在就new一个
        List<String> list = null;
        if (redisTemplate.hasKey(userKey)) {
            String s = redisTemplate.opsForValue().get(userKey);
            list = JSON.parseArray(s, String.class);
        } else {
            list = new ArrayList<>();
        }
        //判断sid是否在集合里，在取消，不在就添加
        StrategyStatisVO vo = this.getStrategyStatisVO(sid);
        if (list.contains(sid)) {
            vo.setFavornum(vo.getFavornum() - 1);
            list.remove(sid);
        } else {
            vo.setFavornum(vo.getFavornum() + 1);
            list.add(sid);
        }
        //更新list和vo对象
        this.setStrategyStatisVO(vo);
        redisTemplate.opsForValue().set(userKey, JSON.toJSONString(list));
        return list.contains(sid);
    }

    @Override
    public List<String> findStrategyidsByuserId(String uid) {
        String uidKey = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        //获取sid集合
        List<String> list = null;
        if (redisTemplate.hasKey(uidKey)) {
            String s = redisTemplate.opsForValue().get(uidKey);
            list = JSON.parseArray(s, String.class);
        } else {
            list = new ArrayList<>();
        }
        return list;
    }

    //点赞操作
    @Override
    public Boolean strategyThumbup(String sid, String uid) {
        //用sid和uid 作为key id
        String key = RedisKeys.STRATEGY_THUMB_UID_SID.join(sid, uid);
        //存在说明今天已经点过赞了
        if (redisTemplate.hasKey(key)) {
            return false;
        }
        //设置时效
        Date now = new Date();
        Date endDate = DateUtil.getEndDate(now);
        long time = DateUtil.getDateBetween(endDate, now);
        //点赞数+1
        StrategyStatisVO vo = this.getStrategyStatisVO(sid);
        vo.setThumbsupnum(vo.getThumbsupnum() + 1);
        redisTemplate.opsForValue().set(key, "haha", time, TimeUnit.SECONDS);
        this.setStrategyStatisVO(vo);
        return true;
    }

    @Override
    public boolean isVOExists(String sid) {
        String vokey = RedisKeys.STRATEGY_VO.join(sid);

        return redisTemplate.hasKey(vokey);
    }

    @Override
    public List<StrategyStatisVO> queryVoByPattern(String pattern) {
        List<StrategyStatisVO> list = new ArrayList<>();
        //获取key集合
        String s = RedisKeys.STRATEGY_VO.join(pattern);
        Set<String> keys = redisTemplate.keys(s);
        if (keys != null && keys.size() > 0) {
            for (String key : keys) {
                String s1 = redisTemplate.opsForValue().get(key);
                //把vo对象添加到vo集合
                list.add(JSON.parseObject(s1, StrategyStatisVO.class));
            }
        }

        return list;
    }


    @Override
    public List<String> querySid(String uid) {
        String uidKey = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        List<String> list = null;
        if (redisTemplate.hasKey(uidKey)) {
            String s = redisTemplate.opsForValue().get(uidKey);
            list = JSON.parseArray(s, String.class);
        }
        return list;

    }

    @Override
    public void getFavors(String uid, List<String> favors) {
        String uidKey = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        redisTemplate.opsForValue().set(uidKey,JSON.toJSONString(favors));
    }

    @Override
    public Object getTodayView(String ownerId) {
        String key = RedisKeys.VISIT_TODAY.join(ownerId);
        if (redisTemplate.hasKey(key)) {
            redisTemplate.opsForValue().increment(key);
        }else {
            //设置时效
            Date now = new Date();
            Date endDate = DateUtil.getEndDate(now);
            long time = DateUtil.getDateBetween(endDate, now);
            redisTemplate.opsForValue().set(key, "1", time, TimeUnit.SECONDS);
        }

        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public Object getTodayView1(String ownerId) {
        String key = RedisKeys.VISIT_TODAY.join(ownerId);
         return redisTemplate.opsForValue().get(key);
    }
}
